<?php

namespace App\Controller\Backend\Agency;

use App\Controller\AbstractController;
use App\Service\Agency\AgencyCompanyService;
use Hyperf\Di\Annotation\Inject;

class AgencyCompanyController extends AbstractController
{
    #[Inject]
    public AgencyCompanyService $agencyCompanyService;

    public function addAgencyCompany(){
        $data = [
            'agency_company_name' => $this->request->post('agency_company_name'),
            'cooperate_status' => $this->request->post('cooperate_status'),
            'remark' => $this->request->post('remark')
        ];
        $validator = $this->validationFactory->make(
            [
                'agency_company_name' => $data['agency_company_name'],
                'cooperate_status' => $data['cooperate_status'],
            ],
            [
                'agency_company_name' => 'required',
                'cooperate_status' => 'required|in:1,2,3,4',
            ],
            [
                'agency_company_name.required' => '委案公司名称不能为空',
                'cooperate_status.required' => '合作状态不能为空',
                'cooperate_status.in' => '合作状态必须正确',
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }
        $res = $this->agencyCompanyService->addAgencyCompany($data);
        return success($res);
    }

    public function getAgencyCompanyList(){
        $filters = [];
        $res = $this->agencyCompanyService->getAgencyCompanyList($filters);
        return success($res);
    }

    public function getAgencyCompanyInfo(){
        $id = $this->request->post('id',0);
        $validator = $this->validationFactory->make(
            [
                'id' => $id
            ],
            [
                'id' => 'required|integer'
            ],
            [
                'id.required' => 'id不能为空',
                'id.integer' => 'id必须为数字',
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }
        $res = $this->agencyCompanyService->getAgencyCompanyInfo($id);
        return success($res);
    }

    public function delAgencyCompany(){
        $ids = $this->request->post('ids',[]);
        $validator = $this->validationFactory->make(
            [
                'ids' => $ids
            ],
            [
                'ids' => 'required'
            ],
            [
                'ids.required' => 'id不能为空',
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }
        return success($this->agencyCompanyService->delAgencyCompany($ids));

    }

    public function updateAgencyCompany(){
        $id = $this->request->post('id');
        $validator = $this->validationFactory->make(
            [
                'id' => $id
            ],
            [
                'id' => 'required'
            ],
            [
                'id.required' => 'id不能为空',
            ]
        );
        if ($validator->fails()){
            apiError($validator->errors()->first());
        }
        return success($this->agencyCompanyService->editAgencyCompany($id,[
            'agency_company_name' => $this->request->post('agency_company_name'),
            'cooperate_status' => $this->request->post('cooperate_status'),
            'remark' => $this->request->post('remark'),
        ]));
    }

    public function getAgencyCompanySelect(){
        return success($this->agencyCompanyService->getAgencyCompanySelect());
    }

}